package com.chinasoftinc.facommon.view.device.abilitypersent;

import ohos.bluetooth.BluetoothHost;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.MatchingSkills;

import com.chinasoftinc.facommon.DeviceControlConstants;
import com.chinasoftinc.facommon.utils.RemoteUtils;
import com.chinasoftinc.facommon.view.device.ControlDeviceAbility;
import com.chinasoftinc.facommon.utils.LogUtil;

/**
 * @since 2022-05-10
 */
public class BleDevicePresent extends DeviceAbilityPresent {
    private BluetoothHost btHost = null;

    public BleDevicePresent(ControlDeviceAbility ability) {
        super(ability);
        btHost = BluetoothHost.getDefaultHost(ability);
    }


    private void sendConnectAction() {
        if (conversationRemoteObj == null || deviceId == null) {
            return;
        }
        RemoteUtils.sendRemoveMessage(
            conversationRemoteObj,
            DeviceControlConstants.ACTION_MESSAGE_CODE_CONNECT,
            null,
            0
        );
    }

    @Override
    protected void addSubscribeFaEvent(MatchingSkills matchingSkills) {
        matchingSkills.addEvent(BluetoothHost.EVENT_HOST_STATE_UPDATE);
        matchingSkills.addEvent(BluetoothHost.HOST_PARAM_CUR_STATE);
    }

    @Override
    protected void onReceiveEvent(String action, CommonEventData commonEventData) {
        if (BluetoothHost.EVENT_HOST_STATE_UPDATE.equals(action)) {
            if (btHost.getBtState() == BluetoothHost.STATE_ON) {
                LogUtil.info(TAG, "onReceiveEvent, start connect device : ");
                sendConnectAction();
            }
        }
    }
}
